<template>
  <div class="home-new" ref='target'>
    <HomePannel title='新鲜好物' subTitle='新鲜出炉，品质靠谱'>
      <template #right>
        <XtxMore />
      </template>
      <div style="position: relative;height: 406px;">
        <Transition name="fade">
          <!-- 商品列表 -->
          <ul v-if="list.length" class="goods-list">
            <li v-for="item in list" :key="item.id">
              <RouterLink :to="`/product/${item.id}`">
                <img :src="item.picture" alt="">
                <p class="name ellipsis">{{item.name}}</p>
                <p class="price">&yen;{{item.price}}</p>
              </RouterLink>
            </li>
          </ul>
          <HomeSkeleton bg="#f0f9f4" v-else />
           <!-- 1.Transition大写时候在v-if和v-else之前不要添加注释，懒加载无效果2.transition小写时候里面可以随意加注释 可以显示懒加载效果3.<transition-group> 官网写法可以显示懒加载效果 -->
          </Transition>
      </div>
    </HomePannel>
  </div>
</template>
<script>

import { findNew } from '@/api/home.js'
// import { ref } from 'vue'
import HomePannel from './home-pannel.vue'
import HomeSkeleton from './home-skeleton.vue'
// import { useIntersectionObserver } from '@vueuse/core'
import { useLazyData } from '@/hook/index.js'

export default {
  name: 'HomeNew',
  components: { HomePannel, HomeSkeleton },
  // async setup () {
  //   const list = ref([])
  //   const ret = await findNew()
  //   console.log(ret)
  //   list.value = ret.result
  //   // async函数默认返回的是Promise实例对象
  //   return { list }
  // }
  setup () {
    // const list = ref([])
    /* findNew().then(ret => {
      setTimeout(() => {
        list.value = ret.result
      }, 1000)
    }) */

    // 监听组件进入了可视区
    // const target = ref(null)
    // const { stop } = useIntersectionObserver(target, ([{ isIntersecting }]) => {
    //   if (isIntersecting) {
    //     // console.log('该组件进入到了可视区')
    //     findNew().then(ret => {
    //       list.value = ret.result
    //       // 获取到数据之后，停止继续监听
    //       stop()
    //     })
    //   }
    // })
    const { target, list } = useLazyData(findNew)
    return { list, target }
  }
}
</script>

<style scoped lang="less">
.goods-list {
  display: flex;
  justify-content: space-between;
  height: 406px;
  li {
    width: 306px;
    height: 406px;
    background: #f0f9f4;
    .hoverShadow();
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding: 12px 30px 0 30px;
      text-align: center;
    }
    .price {
      color: @priceColor;
    }
  }
}
</style>
